package cn.itcast.bos.web.action.teke_delivary;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.take_delivary.Promotion;
import cn.itcast.bos.service.action.teke_delivary.PromotionService;
import cn.itcast.bos.utils.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion> {

	// 注入Service
	@Autowired
	private PromotionService promotionservice;

	// 设置获取图片 不会在实体中和name属性对应的 String 产生关系 避免了因为类型不同 造成的错误异常
	private File titleImgFile;
	private String titleImgFileFileName;

	public void setTitleImgFileFileName(String titleImgFileFileName) {
		this.titleImgFileFileName = titleImgFileFileName;
	}

	public void setTitleImgFile(File titleImgFile) {
		this.titleImgFile = titleImgFile;
	}

	@Action(value = "promotion_savePage", results = {
			@Result(name = "success", type = "redirect", location = "./pages/take_delivery/promotion.html") })
	public String save() throws IOException {
		// 获取图片的 上传路径 / 上传图片的 保存路径
		String savePath = ServletActionContext.getServletContext().getRealPath("/upload/");
		String saveUrl = ServletActionContext.getRequest().getContextPath() + "/upload/";
		// 为图片配置名称
		UUID uuid = UUID.randomUUID();
		String ext = titleImgFileFileName.substring(titleImgFileFileName.lastIndexOf("."));
		String randomFileName = uuid + ext;
		// 保存图片绝对路径到 model 中 通过 model 将图片的绝对路径 保存到 数据库
		File destFile = new File(savePath + "/" + randomFileName);
		System.out.println(destFile.getAbsolutePath()); // ----get到的是什么？？？？
		FileUtils.copyFile(titleImgFile, destFile);

		// 将保存路径 相对工程web访问路径，保存model中
		model.setTitleImg(ServletActionContext.getRequest().getContextPath() + "/upload/" + randomFileName);

		// 调用业务层
		promotionservice.save(model);
		return SUCCESS;
	}

	// 属性驱动
	protected int page;
	protected int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	// 分页 //查询
	@Action(value = "promotion_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		// 进行分页查询
		// 构造分页查询参数
		Pageable pageable = new PageRequest(page - 1, rows);
		// 调用业务层 完成查询
		Page<Promotion> pageData = promotionservice.findPageData(pageable);
		// 压入值栈
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}

	
}
